package com.wholesale;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.Comment;
import com.wholesale.datas.WebServiceClient;
import com.wholesale.utils.Constants;
import com.wholesale.utils.GoodsCommentListAdapter;

public class GoodsCommentsLayout extends LinearLayout implements
		OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private ListView bodyList ;
	private RelativeLayout loadingLayout ;
	private GoodsCommentListAdapter goodsCommentListAdapter ;
	private int goodsId;

	// 构造函数
	public GoodsCommentsLayout(Context context, final ViewFlipper viewFlipper,
			int goodsId) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		this.goodsId = goodsId;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.common, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Detail");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setText("+");
		rightButton.setOnClickListener(this);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("");
		loadingLayout = (RelativeLayout)baseView.findViewById(R.id.loadingLayout);
		bodyList = (ListView)baseView.findViewById(R.id.bodyList);
		goodsCommentListAdapter = new GoodsCommentListAdapter(context, bodyList, this.goodsId);
		bodyList.setAdapter(goodsCommentListAdapter);
		GoodsCommentsAsync goodsCommentsAsync = new GoodsCommentsAsync(context);
		goodsCommentsAsync.execute(this.goodsId);
	}

	class GoodsCommentsAsync extends AsyncTask<Integer, Integer, ArrayList<Comment>> {
		ProgressDialog pdialog;
		int pageCount = 0 ; 
		
		public GoodsCommentsAsync(Context context) {
		}

		@Override
		protected void onPreExecute() {
			loadingLayout.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected ArrayList<Comment> doInBackground(Integer... params) {
			pageCount = WebServiceClient.getCommentsPageCount(params[0]).intValue();
			if(pageCount != 0){
				return WebServiceClient.getComments(params[0], 1);
			}else{
				return new ArrayList<Comment>();
			}
		}
		@Override
		protected void onPostExecute(ArrayList<Comment> result) {
			goodsCommentListAdapter.addItems(result);
			goodsCommentListAdapter.setAsyncLoadData(pageCount);
			goodsCommentListAdapter.notifyDataSetChanged();
//			pdialog.dismiss();
			loadingLayout.setVisibility(View.GONE);
		}

		@Override
		protected void onCancelled() {
			super.onCancelled();
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:
			if(Constants.userInfo == null){
				UserLoginLayout userLoginLayout = new UserLoginLayout(context, viewFlipper);
				viewFlipper.addView(userLoginLayout);
				viewFlipper.showNext();
			}else{
				AddCommentLayout addCommentLayout = new AddCommentLayout(context, viewFlipper, goodsId);
				viewFlipper.addView(addCommentLayout);
				viewFlipper.showNext();
			}
			break;
		default:
			break;
		}

	}
}
